package com.lkd.job;

import com.lkd.common.VMSystem;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Component
@Slf4j
public class UserJob {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 工单列表初始化
     * @param pram
     * @return
     */
    @XxlJob("workCountIntJobHandler")
    public ReturnT<String> workCountIntJobHandler(String pram){
        try {
            log.info("每日工单列表初始化");
            userService.list().forEach(user->{
                if (!user.getRole().equals("1001")){
                    String key = VMSystem.REGION_TASK_KEY_PREF+ LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
                            //区域
                            +"."+user.getRegionId()
                            //编号
                            +"."+user.getRoleCode();
                    //保存在redis里面
                    redisTemplate.opsForZSet().add(key,user.getId(),0);
                    //指定redis过期时间
                    redisTemplate.expire(key, Duration.ofDays(1));
                }
            });
            return ReturnT.SUCCESS;
        } catch (Exception e) {
           return ReturnT.FAIL;
        }
    }
}
